import { GlobalEvent } from "./GlobalEvent";
import { ConstData } from "../base/ConstData";
import { guideData } from "../configData/guideData";

const {ccclass, property} = cc._decorator;

@ccclass
export default class GuideMgr extends cc.Component {

    @property(cc.Prefab)
    guidePrefab:cc.Prefab = null;
    @property()
    runSpeed:number = 0.2;
    @property()
    enable:boolean = true;
    @property()
    curStep:number = 0;

    guideLayer:cc.Node = null;
    maskLayer:cc.Node = null;
    maskNode:cc.Node = null;
    tipsLabel:cc.Label = null;

    curPreFunc:any = null;
    curGuideDataItem:any = null;
    curGuideNode:cc.Node = null;
    curEndFunc:any = null;
    isAppeard:boolean = false;
    startShowFunc:any = null;
    endShowFunc:any = null;

    onLoad() {
        cc.game.addPersistRootNode(this.node);
        this.initShowInfoFuncMap();
        this.startGuide();
    }

    startGuide()
    {
        if(this.enable)
        {
            if(cc.sys.localStorage.getItem("isGuided")){
                this.endGuide();
                return;
            }
            this.intoStep(this.curStep);
            this.schedule(this.Process, this.runSpeed);
        }
    }

    //结束引导
    endGuide()
    {
        if(cc.find("Canvas/guide"))
            cc.find("Canvas/guide").destroy();
        this.unschedule(this.Process);
        cc.sys.localStorage.setItem("isGuided", true);
    }

    intoStep(step:number)
    {
        if(guideData[step])
        {
            cc.log("进入步骤:" + step)
            this.curGuideDataItem = guideData[step];
            this.setPreFunc();  //设置前置函数
            this.setEndFunc();  //设置完成函数
            this.setShowFunc(); //设置显示函数
        }
        else
        {
            cc.log("引导结束");
            this.endGuide();
        }
    }

    setPreFunc()
    {
        var str = "";
        for(var i =0, len = this.curGuideDataItem.preFunc.length; i < len - 1; i++)
        {
            str += this.curGuideDataItem.preFunc[i] + "&&";
        }
        str += this.curGuideDataItem.preFunc[this.curGuideDataItem.preFunc.length-1]

        this.curPreFunc = str;
    }

    //设置完成函数
    setEndFunc()
    {
        var str = "";
        for(var i =0, len = this.curGuideDataItem.endFunc.length; i < len - 1; i++)
        {
            str += this.curGuideDataItem.endFunc[i] + "&&";
        }
        str += this.curGuideDataItem.endFunc[this.curGuideDataItem.endFunc.length-1]
        
        if(this.curGuideDataItem.talkIndex)
        {
            str += "&&" + 'cc.find("guideMgr")';
            str += "&&" + 'cc.find("guideMgr").getComponent("GuideMgr")._talkEnd == true';
        }

        this.curEndFunc = str;
    }

    setShowFunc()
    {
        if(this.showInfoFuncMap[this.curStep])
        {
            this.startShowFunc = function(){
                this.isAppeard = true;
                this.showInfoFuncMap[this.curStep].startFunc();
            }
            this.endShowFunc = function(){
                this.isAppeard = false;
                this.showInfoFuncMap[this.curStep].endFunc();
            }
        }
        else
        {
            if(this.curGuideDataItem.talkIndex)
            {
                this.startShowFunc = function(){
                    this.isAppeard = true;
                    this.showTalkBox();
                }

                this.endShowFunc = function(){
                    this.isAppeard = false;
                    this.endshowTalkBox();
                }
            }
            else
            {
                this.startShowFunc = function(){
                    this.isAppeard = true;
                    this.defaultShowInfo();
                }
                this.endShowFunc = function(){
                    this.isAppeard = false;
                }
            }
        }    
    }

    Process()
    {
        this.foundGuideNode();
        this.checkEnd();
    }

    checkEnd()
    {
        if(eval(this.curEndFunc))
        {
            cc.log("完成步骤：" + this.curStep);
            if(this.isAppeard && this.endShowFunc)
                this.endShowFunc();
            this.curStep++;
            this.intoStep(this.curStep);
        }
    }

    foundGuideNode()
    {
        if(eval(this.curPreFunc))
        {
            this.curGuideNode = eval(this.curGuideDataItem.guideNode);
            this.createGuideLayer(); //创建引导层
            this.guideLayer.active = true;
            this.setMaskNode(); //设置遮罩点
            this.setMaskLayer(); //设置黑色遮罩层
            if(!this.isAppeard && this.startShowFunc)
                this.startShowFunc();
        }
        else
        {
            if(this.isAppeard && this.endShowFunc)
                this.endShowFunc();
        }
    }

    //创建引导层
    createGuideLayer()
    {
        if(cc.find("Canvas/guide")) return;
        this.guideLayer = cc.instantiate(this.guidePrefab);
        this.maskLayer = this.guideLayer.getChildByName("maskLayer");
        this.maskNode = this.guideLayer.getChildByName("maskNode");
        this.tipsLabel = this.guideLayer.getChildByName("tips").getComponent(cc.Label);
        this.guideLayer.on(cc.Node.EventType.TOUCH_START, this.onTouch, this);
        this.guideLayer.on(cc.Node.EventType.TOUCH_MOVE, this.onTouch, this);
        this.guideLayer.on(cc.Node.EventType.TOUCH_END, this.onTouch, this);
        this.guideLayer.parent = cc.find("Canvas");
        this.guideLayer.zIndex = cc.macro.MAX_ZINDEX;
    }

    onTouch(event) {
        let point:cc.Vec2 = event.getLocation();
        let rectTouchabel:cc.Rect = this.maskNode.getBoundingBoxToWorld();
        rectTouchabel.width = rectTouchabel.width <= 0 ? 0 : rectTouchabel.width;
        rectTouchabel.height = rectTouchabel.height <= 0 ? 0 : rectTouchabel.height;
        if (rectTouchabel.contains(point)) {
            this.guideLayer._touchListener.setSwallowTouches(false);
        } else {
            this.guideLayer._touchListener.setSwallowTouches(true);
        }
    }

    //设置遮罩点
    setMaskNode()
    {
        this.maskNode.active = true;
        var pos1 = this.curGuideNode.convertToWorldSpaceAR(cc.v2(0,0));
        var pos2 = this.maskNode.parent.convertToNodeSpaceAR(pos1);
        this.maskNode.setPosition(pos2);

        this.maskNode.width = typeof this.curGuideDataItem.maskWidth == "string" ? eval(this.curGuideDataItem.maskWidth) : this.curGuideDataItem.maskWidth;
        this.maskNode.height = typeof this.curGuideDataItem.maskHeight == "string" ? eval(this.curGuideDataItem.maskHeight) : this.curGuideDataItem.maskHeight;
        
        if(this.curGuideDataItem.maskOffsetX)
        {
            this.maskNode.setPosition(this.maskNode.position.x + this.curGuideDataItem.maskOffsetX, this.maskNode.position.y);
        }

        if(this.curGuideDataItem.maskOffsetY)
        {
            this.maskNode.setPosition(this.maskNode.position.x , this.maskNode.position.y + this.curGuideDataItem.maskOffsetY);
        }

        //无限制节点
        if(this.curGuideDataItem.guideNode == 'cc.find("Canvas")')
        {
            this.maskNode.setPosition(0,0);
        }
    }

    //设置黑色遮罩层
    setMaskLayer()
    {
        this.maskLayer.active = true;
        this.maskLayer.setPosition(this.maskNode.position);
        this.maskLayer.width = this.maskNode.width;
        this.maskLayer.height = this.maskNode.height;
    }

    //自定义步骤特殊提示
    showInfoFuncMap:{[key:number]:ShowInfoFunc} = {};
    initShowInfoFuncMap()
    {
    }

    //默认显示
    defaultShowInfo()
    {
        this.tipsLabel.string = this.curGuideDataItem.tips;
        this.tipsLabel.node.active = true;
    }
    //对话框显示
    showTalkBox()
    {
        this.tipsLabel.node.active = false;
        this["_talkEnd"] = false;
        GlobalEvent.emit(GlobalEvent.Event.openPanel, {PanelId: ConstData.panelID.talkBox, Data:this.curGuideDataItem.talkIndex});
        GlobalEvent.on(GlobalEvent.Event.onPanelClose, function(Event){
            if(Event.PanelId == ConstData.panelID.talkBox)
                this["_talkEnd"] = true;
        }.bind(this), this);
    }
    //对话框结束
    endshowTalkBox()
    {
    }
}

type ShowInfoFunc = {
    startFunc:any;
    endFunc:any;
}
